<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货物类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择货物类型" clearable @change="handleChangeGoodsType">
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产地" prop="product">
        <el-select v-model="queryParams.product" placeholder="请选择产地" clearable @change="handleChangeProduct">
          <el-option
            v-for="dict in productOptions"
            :key="dict.goodsTheoryWeightId"
            :label="dict.product"
            :value="dict.product"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-select v-model="queryParams.specification" placeholder="请选择规格" clearable @change="handleChangeSpecification">
          <el-option
            v-for="dict in specificationOptions"
            :key="dict.goodsTheoryWeightId"
            :label="dict.specification"
            :value="dict.specification"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="材质" prop="steelGrade">
        <el-select v-model="queryParams.steelGrade" placeholder="请选择材质" clearable>
          <el-option
            v-for="dict in steelGradeOptions"
            :key="dict.goodsTheoryWeightId"
            :label="dict.steelGrade"
            :value="dict.steelGrade"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重量" prop="theoryWeight">
        <el-input
          v-model="queryParams.theoryWeight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="单位" prop="unit">
        <el-select v-model="queryParams.unit" placeholder="请选择单位" clearable>
          <el-option
            v-for="dict in dict.type.goods_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="件数" prop="numberOfPiece">
        <el-input
          v-model="queryParams.numberOfPiece"
          placeholder="请输入件数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入显示顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用状态" prop="useStatus">
        <el-select v-model="queryParams.useStatus" placeholder="请选择使用状态" clearable>
          <el-option
            v-for="dict in dict.type.use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="园区代码" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:theoryweight:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:theoryweight:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:theoryweight:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:theoryweight:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="theoryweightList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:theoryweight:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:theoryweight:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <!--el-table-column label="货物理重ID" align="center" prop="goodsTheoryWeightId" /-->
      <el-table-column label="货物类型" align="center" prop="goodsType" />
      <el-table-column label="产地" align="center" prop="product" />
      <el-table-column label="规格" align="center" prop="specification" width="110" :show-overflow-tooltip="true" />
      <el-table-column label="材质" align="center" prop="steelGrade" width="110" :show-overflow-tooltip="true" />
      <el-table-column label="重量" align="center" prop="theoryWeight" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="件数" align="center" prop="numberOfPiece" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="使用状态" align="center" prop="useStatus" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货物理重对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="货物类型" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择货物类型" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产地" prop="product">
          <el-input v-model="form.product" placeholder="请输入产地" clearable />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" clearable />
        </el-form-item>
        <el-form-item label="材质" prop="steelGrade">
          <el-input v-model="form.steelGrade" placeholder="请输入材质" clearable />
        </el-form-item>
        <el-form-item label="重量" prop="theoryWeight">
          <el-input v-model="form.theoryWeight" placeholder="请输入重量" clearable />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择单位" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.goods_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="件数" prop="numberOfPiece">
          <el-input v-model="form.numberOfPiece" placeholder="请输入件数" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" style="width: 340px;" />
        </el-form-item>
        <el-form-item label="使用状态" prop="useStatus">
          <el-select v-model="form.useStatus" placeholder="请选择使用状态" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.use_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="园区代码" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入园区代码" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTheoryweight, getTheoryweight, delTheoryweight, addTheoryweight, updateTheoryweight, listProductOption, listSpecificationOption, listSteelGradeOption } from "@/api/system/theoryweight";
import { isNotNull } from "@/utils/validate";

export default {
  name: "Theoryweight",
  dicts: ['goods_unit', 'use_status', 'goods_type'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货物理重表格数据
      theoryweightList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 园区代码时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsType: null,
        product: null,
        specification: null,
        steelGrade: null,
        theoryWeight: null,
        unit: null,
        numberOfPiece: null,
        remark: null,
        orderNum: null,
        useStatus: null,
        parkCode: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 字典项产地
      productOptions: [],
      // 字典项规格
      specificationOptions: [],
      // 字典项材质
      steelGradeOptions: [],
      // 表单校验
      rules: {
        goodsType: [
          { required: true, message: "货物类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询货物理重列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listTheoryweight(this.queryParams).then(response => {
        this.theoryweightList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsTheoryWeightId: null,
        goodsType: null,
        product: null,
        specification: null,
        steelGrade: null,
        theoryWeight: null,
        unit: null,
        numberOfPiece: null,
        remark: null,
        orderNum: 0,
        useStatus: "正常",
        dataRealm: null,
        parkCode: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsTheoryWeightId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货物理重";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsTheoryWeightId = row.goodsTheoryWeightId || this.ids
      getTheoryweight(goodsTheoryWeightId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货物理重";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsTheoryWeightId != null) {
            updateTheoryweight(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTheoryweight(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsTheoryWeightIds = row.goodsTheoryWeightId || this.ids;
      this.$modal.confirm('是否确认删除货物理重编号为"' + goodsTheoryWeightIds + '"的数据项？').then(function() {
        return delTheoryweight(goodsTheoryWeightIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/theoryweight/export', {
        ...this.queryParams
      }, `theoryweight_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    },
    handleChangeGoodsType(value) {
//      console.log('handleChangeGoodsType', value);
      let query = {
        goodsType: value,
        product: null,
        specification: null,
        steelGrade: null,
        useStatus: "正常",
       };
      
      if (isNotNull(value)) {
        /** 字典项产地 */
        listProductOption(query).then(response => {
          this.productOptions = response.data;
          this.specificationOptions = {};
          this.steelGradeOptions = {};
//          console.log('this.productOptions', this.productOptions);
        });
      } else {
        this.productOptions = {};
        this.specificationOptions = {};
        this.steelGradeOptions = {};
      }

    },
    handleChangeProduct(value) {
//      console.log('handleChangeProduct', value);
      let query = {
        goodsType: this.queryParams.goodsType,
        product: value,
        specification: null,
        steelGrade: null,
        useStatus: "正常",
       };
      
      if (isNotNull(value)) {
        /** 字典项规格 */
        listSpecificationOption(query).then(response => {
          this.specificationOptions = response.data;
          this.steelGradeOptions = {};
//          console.log('this.specificationOptions', this.specificationOptions);
        });
      } else {
        this.specificationOptions = {};
        this.steelGradeOptions = {};
      }

    },
    handleChangeSpecification(value) {
//      console.log('handleChangeSpecification', value);
      let query = {
        goodsType: this.queryParams.goodsType,
        product: this.queryParams.product,
        specification: value,
        steelGrade: null,
        useStatus: "正常",
       };
      
      if (isNotNull(value)) {
        /** 字典项材质 */
        listSteelGradeOption(query).then(response => {
          this.steelGradeOptions = response.data;
//          console.log('this.steelGradeOptions', this.steelGradeOptions);
        });
      } else {
        this.steelGradeOptions = {};
      }

    },
  }
};
</script>
